
#import "NSArray+YsToolsManager.h"
#import "NSDictionary+YsToolsManager.h"

@implementation NSArray (YsToolsManager)

+ (instancetype)qmui_arrayWithObjects:(id)object, ... {
    void (^addObjectToArrayBlock)(NSMutableArray *array, id obj) = ^void(NSMutableArray *array, id obj) {
        if ([obj isKindOfClass:[NSArray class]]) {
            [array addObjectsFromArray:obj];
        } else {
            [array addObject:obj];
        }
    };
    
    NSMutableArray *result = [[NSMutableArray alloc] init];
    addObjectToArrayBlock(result, object);
    
    va_list argumentList;
    va_start(argumentList, object);
    id argument;
    while ((argument = va_arg(argumentList, id))) {
        addObjectToArrayBlock(result, argument);
    }
    va_end(argumentList);
    if ([self isKindOfClass:[NSMutableArray class]]) {
        return result;
    }
    return result.copy;
}

- (void)qmui_enumerateNestedArrayWithBlock:(void (^)(id, BOOL *))block {
    BOOL stop = NO;
    for (NSInteger i = 0; i < self.count; i++) {
        id object = self[i];
        if ([object isKindOfClass:[NSArray class]]) {
            [((NSArray *)object) qmui_enumerateNestedArrayWithBlock:block];
        } else {
            block(object, &stop);
        }
        if (stop) {
            return;
        }
    }
}

- (NSMutableArray *)qmui_mutableCopyNestedArray {
    NSMutableArray *mutableResult = [self mutableCopy];
    for (NSInteger i = 0; i < self.count; i++) {
        id object = self[i];
        if ([object isKindOfClass:[NSArray class]]) {
            NSMutableArray *mutableItem = [((NSArray *)object) qmui_mutableCopyNestedArray];
            [mutableResult replaceObjectAtIndex:i withObject:mutableItem];
        }
    }
    return mutableResult;
}

- (NSArray *)qmui_filterWithBlock:(BOOL (^)(id))block {
    if (!block) {
        return self;
    }
    
    NSMutableArray *result = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < self.count; i++) {
        id item = self[i];
        if (block(item)) {
            [result addObject:item];
        }
    }
    return [result copy];
}

- (NSArray *)qmui_mapWithBlock:(id (^)(id item))block {
    if (!block) {
        return self;
    }
    
    NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:self.count];
    for (NSInteger i = 0; i < self.count; i++) {
        [result addObject:block(self[i])];
    }
    return [result copy];
}

- (NSMutableArray *)mutableArrayDeeoCopy {
    NSMutableArray * array = [NSMutableArray array];
    [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        id objOject;
        
        if ([obj isKindOfClass:[NSDictionary class]]) {
            objOject = [obj mutableDicDeepCopy];
            
        }else if ([obj isKindOfClass:[NSArray class]]){
            
            objOject = [obj mutableArrayDeeoCopy];
            
        }else{
            
            objOject = obj;
        }
        [array addObject:objOject];
        
    }];
    
    return array;
}

- (BOOL)qmui_isEqualToArray:(NSArray *)otherArray {
    NSSet *set1 = [[NSSet alloc] initWithArray:self];
    NSSet *set2 = [[NSSet alloc] initWithArray:otherArray];
    return [set1 isEqualToSet:set2];
}

- (BOOL)isEmpty {
    return self.count<=0;
}

-(NSString *(^)(NSString *separator))joinedByString{
    return  ^(NSString *separator) {
        return  [self componentsJoinedByString:separator];
    };
}

- (NSInteger)qmui_firstIndexWithBlock:(BOOL (^)(id item))block
{
    NSInteger index = -1;
    if (!block) return index;
    for (NSInteger i = 0; i < self.count; i++) {
        if (block(self[i])) {
            index = i;
            break;
        }
    }
    return index;
}


- (id)qmui_firstObjectWithBlock:(BOOL (^)(id item))block
{
    id obj = nil;
    if (!block) return obj;
    for (NSInteger i = 0; i < self.count; i++) {
        if (block(self[i])) {
            obj = self[i];
            break;
        }
    }
    return obj;
}

- (NSArray *)qmui_removeDuplicateWithBlock:(BOOL (^)(id,id))block {
    if (!block) return self;
    NSMutableSet *result = [[NSMutableSet alloc] init];
    for (NSInteger i = 0; i < self.count; i++) {
        for (NSInteger j = i; j < self.count; j++) {
            id item1 = self[i];
            id item2 = self[j];
            if (i == j) continue;
            if (block(item1,item2)) {
                [result addObject:item2];
            }
        }
    }
    NSMutableArray *original =  self.mutableCopy;
    [original removeObjectsInArray:result.allObjects];
    return original;
}

@end
